package com.fottas.adware.payment.action;

import com.fottas.adware.common.api.IErrorCode;
import com.fottas.adware.common.emun.PayTypeEnum;
import com.fottas.adware.common.exception.ApiException;
import com.fottas.adware.factory.WxFactory;
import com.fottas.adware.payment.model.vo.request.PayRequest;
import com.fottas.adware.util.WxPayUtils;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service("wxPayAction")
public class WxPayAction implements PayAction {


    @Override
    public String doAction(PayRequest request) {
        try {
            String xmlResult = WxFactory.Payment.App().pay(null, false, null);
            Map<String, String> result = WxPayUtils.xmlToMap(xmlResult);

            String returnCode = result.get("return_code");
            String returnMsg = result.get("return_msg");
            if (!WxPayUtils.codeIsOk(returnCode)) {
                throw ApiException.create(IErrorCode.createCodeMessage(returnCode, returnMsg));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } return null;
    }

    @Override
    public PayTypeEnum getPayType() {
        return PayTypeEnum.WX;
    }
}